package Model
{
	import flash.events.Event;
	import mx.core.FlexGlobals;
	import mx.events.PropertyChangeEvent;
	
	import Controller.AreasController;

	public class SolidComponent extends SimpleComponent
	{
		
		private var _length:Number;
		private var _height:Number;
		private var _area:Number;
		private var _estimation:Number;
		private var _construction:Construction;
		private var propertyChangedEvent:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
		protected var areaController:AreasController = new AreasController();
		//counters for id generator
		public static var wallToAirCounter:Number = 1;
		public static var wallToGroundCounter:Number = 1;
		public static var roofSlopedCounter:Number = 1;
		public static var roofFlatCounter:Number = 1;
		public static var slabCounter:Number = 1;
		public static var slabToUnheatedCellarCounter:Number = 1;
		
		public function SolidComponent(description:String, quantity:Number=1, length:Number = 0, height:Number = 0, estimation:Number = 0, area:Number = 0)
		{	
			super(description, quantity);
			this.id = generateId();
			this._length = length;
			this._height = height;
			this._estimation = estimation;
			this._area = area;
			this.addEventListener("internalChange", handleInternalChange);
			this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, handleProperyChangedEvent);
		}
		
		override public function generateId():String
		{
			var result:String;
			
			if(description == resourceManager.getString('systemtexts','exteriorWallToAmbientAir'))
			{
				if(wallToAirCounter < 9)
				{	
					result = "EW0" + wallToAirCounter.toString();
				}
				else
				{
					result = "EW" + wallToAirCounter.toString();
				}
				
				wallToAirCounter ++;
			}
			else if(description == resourceManager.getString('systemtexts','exteriorWallToGround'))
			{
				if(wallToGroundCounter < 9)
				{	
					result = "EG0" + wallToGroundCounter.toString();
				}
				else
				{
					result = "EG" + wallToGroundCounter.toString();
				}
				
				wallToGroundCounter++;
			}
			else if(description == resourceManager.getString('systemtexts','roofAreaSloped'))
			{
				if(roofSlopedCounter < 9)
				{	
					result = "RS0" + roofSlopedCounter.toString();
				}
				else
				{
					result = "RS" + roofSlopedCounter.toString();
				}
				
				roofSlopedCounter++;
			}
			else if(description == resourceManager.getString('systemtexts','roofAreaFlat'))
			{
				if(roofFlatCounter < 9)
				{	
					result = "RF0" + roofFlatCounter.toString();
				}
				else
				{
					result = "RF" + roofFlatCounter.toString();
				}
				
				roofFlatCounter++;
			}
			else if(description == resourceManager.getString('systemtexts','groundSlab'))
			{
				if(slabCounter < 9)
				{	
					result = "GS0" + slabCounter.toString();
				}
				else
				{
					result = "GS" + slabCounter.toString();
				}
				
				slabCounter++;
			}
			else if(description == resourceManager.getString('systemtexts','groundSlabAgainstCellar'))
			{
				if(slabToUnheatedCellarCounter < 9)
				{	
					result = "GC0" + slabToUnheatedCellarCounter.toString();
				}
				else
				{
					result = "GC" + slabToUnheatedCellarCounter.toString();
				}
				
				slabToUnheatedCellarCounter++;
			}
			
			return result;
		}

		public function get estimation():Number
		{
			return _estimation;
		}

		public function set estimation(value:Number):void
		{
			_estimation = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get length():Number
		{
			return _length;
		}

		public function set length(value:Number):void
		{
			_length = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get height():Number
		{
			return _height;
		}

		public function set height(value:Number):void
		{
			_height = value;
			dispatchEvent(this.internalChangeEvent);
		}

		public function get area():Number
		{
			return _area;
		}

		public function set area(value:Number):void
		{
			_area = value;
			this.propertyChangedEvent.property = this.area;
			dispatchEvent(this.propertyChangedEvent);
		}
		
		override protected function calculateArea():void
		{
			this.area = (this.quantity * (this.length * this.height)) + this.estimation;
		}
		
		protected function handleInternalChange(event:Event):void
		{
			this.calculateArea();
			this.areaController.updateAreaOverviewStatus();
		}
	
		protected function handleProperyChangedEvent(event:PropertyChangeEvent):void
		{
			if(event.property == this.area)
			{
				this.areaController.updateSolidComponentAreaField(this);
				FlexGlobals.topLevelApplication.overviewPalette.updateAreaInputSectionData();
			}
			else if(event.property == this.construction)
			{
				this.areaController.updateOpaqueOverviewStatus();
			}
			FlexGlobals.topLevelApplication.resetCalculations();
		}
		
		public function get construction():Construction
		{
			return _construction;
		}

		public function set construction(value:Construction):void
		{
			_construction = value;
			this.propertyChangedEvent.property = this.construction;
			dispatchEvent(this.propertyChangedEvent);
		}


	}
}